package com.itheima.service.mongo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

@Component
public class RedisIDService {
    @Autowired
    private RedisTemplate redisTemplate;

    /*用redis的自增器来获取一个pid*/
    public Long getNextId(String collectionName){
        //创建一个long类型的原子自增器
        RedisAtomicLong redisAtomicLong = new RedisAtomicLong("id:" + collectionName, redisTemplate.getConnectionFactory() );
        //自增+1
        return redisAtomicLong.incrementAndGet();
    }
}
